#ifndef __LCD_DRIVER_H
#define __LCD_DRIVER_H

#ifdef __cplusplus
extern "C" {
#endif

#include "spi.h"

/*
 * 2.4-inch SPI LCD module without touch
 * LCD controller: ILI9341-compatible
 * Native resolution: 240 x 320, default demo orientation: 320 x 240
 */

#define LCD_CS_0       HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
#define LCD_CS_1       HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET)
#define LCD_DC_0       HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET)
#define LCD_DC_1       HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET)
#define LCD_RST_0      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET)
#define LCD_RST_1      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET)

#define LCD_W          320U
#define LCD_H          240U
#define LCD_SIZE       (LCD_W * LCD_H * 2U)

#define WHITE          0xFFFFU
#define BLACK          0x0000U
#define BLUE           0x001FU
#define GBLUE          0x07FFU
#define RED            0xF800U
#define MAGENTA        0xF81FU
#define GREEN          0x07E0U
#define CYAN           0x7FFFU
#define YELLOW         0xFFE0U
#define BROWN          0xBC40U
#define BRRED          0xFC07U
#define GRAY           0x8430U
#define ORANGE         0xFBE0U
#define PURPLE         0x881FU
#define BRED           0xF81FU
#define GRED           0xFFE0U

#define BACKGROUND_COLOR WHITE

typedef struct
{
    uint16_t width;
    uint16_t height;
    uint8_t dir;
    uint8_t wramcmd;
    uint8_t setxcmd;
    uint8_t setycmd;
} _lcd_dev;

extern _lcd_dev lcddev;
extern uint16_t POINT_COLOR;
extern uint16_t BACK_COLOR;

#define L2R_U2D        0U
#define L2R_D2U        1U
#define R2L_U2D        2U
#define R2L_D2U        3U
#define U2D_L2R        4U
#define U2D_R2L        5U
#define D2U_L2R        6U
#define D2U_R2L        7U

/* Same orientation as the supplied 2.4-inch Arduino reference code. */
#define DFT_SCAN_DIR   U2D_L2R

void Lcd_Gpio_Init(void);
void Lcd_Init(void);
void LCD_Scan_Dir(uint8_t dir);
void LCD_Display_Dir(uint8_t dir);
void LCD_Clear(uint16_t color);
void LCD_Clearx(void);
void Draw_Point(uint16_t x, uint16_t y, uint16_t color);
void BlackLight_SetValue(uint16_t value);
void Spi_write_word(uint16_t data);
void LCD_Set_Cursor(uint16_t x, uint16_t y);
/* end_x/end_y are exclusive. */
void Set_Window(uint16_t start_x, uint16_t start_y, uint16_t end_x, uint16_t end_y);
void Gui_fill_color(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);

#ifdef __cplusplus
}
#endif

#endif
